<?php $show_dialog = true;
include THEMES_PATH . 'public/before_body2.phtml';?>
<link href="/statics/front/css/cart.css" rel="stylesheet">
</head>
<body>
<?php include THEMES_PATH . 'public/header2.phtml';?>
<div class="main-width heading">
  <strong><?php echo L('_ORDER_INFORMATION_');?></strong>
</div>
<form action="<?php echo url('cart/checkout'); ?>" method="post" name="checkoutForm" id="checkoutForm">
  <input type="hidden" name="goods_amount" id="goods_amount" value="<?php echo isset($cartInfo['total_amount']) ? $cartInfo['total_amount'] : '0'; ?>" />
  <input type="hidden" name="goods_number" id="goods_number" value="<?php echo isset($cartInfo['total_number']) ? $cartInfo['total_number'] : '0'; ?>" />
  <input type="hidden" name="goods_weight" id="goods_weight" value="<?php echo isset($cartInfo['total_weight']) ? $cartInfo['total_weight'] : '0'; ?>" />
  <input type="hidden" name="fromcart" value="<?php echo isset($fromcart) ? $fromcart : 0?>" />
  <input type="hidden" name="ordertoken" value="<?php echo isset($ordertoken) ? $ordertoken : 0?>" />
  <input type="hidden" name="delivery_cid" id="delivery_cid" value="1" />
  <input type="hidden" name="delivery_pid" id="delivery_pid" />
  <input name="shipping_fee" id="input_shipping_money" type="hidden" value="0" />
  <input name="payment_fee" id="input_payment_money" type="hidden" value="0" />
  <input type="hidden" name="surplus_money" id="input_surplus_money" value="0" />
  <input type="hidden" name="integral_money" id="input_integral_money" value="0" />
  <input type="hidden" name="hongbao_money" id="input_hongbao_money" value="0" />
  <input type="hidden" name="hongbao_id" id="hongbao_id" value="0" />
  <input type="hidden" name="coupon_money" id="coupon_code_money" value="0" />
  <input type="hidden" name="coupon_code" id="coupon_code" value="0" />
  <input type="hidden" name="adelivery_address_id" id="adelivery_address_id" value="0" />
  <div class="main-width clearfix">
    <div class="col-md-8 row">
      <dl class="definition-list address-list js-address-list">
        <dt class="caption"><input type="hidden" name="address_id" value="<?php echo $addressList ? $addressList[0]['id'] : '';?>" /><i>1</i><?php echo L('_DELIVERY_ADDRESS_');?></dt>
        <?php
        $delivery_cid = 0;
        if(isset($addressList) && $addressList):
            foreach ($addressList as $k => $address):
                if($k==0) $delivery_cid = $address['delivery_cid'];
        ?>
        <dd<?php if($k==0) echo ' class="selected"';?> data-info="<?php echo $address['id'];?>">
          <p class="text"><?php echo L('_checkout_name_');?> <?php echo $address['delivery_first_name'].' '.$address['delivery_last_name'];?></p>
          <p><?php echo L('_checkout_address_');?> <?php echo $address['delivery_province'].' '.$address['delivery_city'].' '.$address['delivery_address'].' '.$address['delivery_address2'];?></p>
          <p><?php echo L('_checkout_tel_');?> <?php echo $address['delivery_phone'];?></p>
          <p class="clearfix align-right address-action">
            <span class="js-edit-address"><?php echo L('edit');?></span>
            <?php if($k!=0): ?><span class="js-delete-address"><?php echo L('delete');?></span><?php endif; ?>
            <?php if($k==0) echo L('_default_address_');?>
          </p>
          <i class="mark"></i>
        </dd>
        <?php
            endforeach;
          endif;
        ?>
        <dd class="add-address-box">
          <span class="align-center add-address js-add-address"><i>＋</i><?php echo L('new_address');?></span>
        </dd>
      </dl>
      <dl class="definition-list shipping-method js-shipping-method">
        <dt class="caption"><input type="hidden" name="shipping_method" value="<?php foreach($shippingList as $shipping) : if ($shipping['status']){echo $shipping['name'];break;};endforeach;?>" /><i>2</i><?php echo L('shipping_method');?></dt>
        <?php
         if(isset($shippingList) && $shippingList):
            $i=0;
            foreach ($shippingList as $key=>$shipping):
             if (!$shipping['status']) continue;
                $i++;
        ?>
        <dd<?php if ($key==0){?> class="selected"<?php } ?> data-info="<?php echo $shipping['name'];?>">
          <p class="text"><?php echo isset($shipping['title']) ? $shipping['title'] : null;?></p>
          <p class="align-center title"><?php echo $shipping['day_or_time']?></p>
          <p><?php echo $shippingList[$key]['description'];?></p>
          <i class="mark"></i>
        </dd>
        <?php
            endforeach;
          endif;
        ?>
      </dl>
      <dl class="definition-list payment-method js-payment-method">
        <dt class="caption">
          <input type="hidden" name="payment_method" value="<?php foreach($paymentList as $payment) : if ($payment['status']){echo $payment['name'];break;};endforeach;?>">
          <i>3</i>
          <?php echo L('payment_method');?>
        </dt>
        <!-- 使用余额支付 -->
        <?php if(isset($user['id']) && $user['id'] && floatval($user['user_money']) > 0 && C('is_use_surplus_money')):?>
        <dt class="use-balance">
          <input type="checkbox" id="is_user_money" name="is_user_money" value="1">
          <label>
            <?php echo L('use_balance_to_pay');?>:
            <strong class="pr1x surplus_money"><?php echo currency(0);?></strong>
            <?php echo L('balance_amount');?>: <strong><?php echo currency($user['user_money']);?></strong>
          </label>
          <span><i class="mark"></i></span>
        </dt>
        <?php endif;?>
        <?php
          if(isset($paymentList) && $paymentList):
          $i=1;
          foreach ($paymentList as $k => $payment) :
            if (!$payment['status']) continue;
        ?>
        <dd<?php if ($i==1){?> class="selected"<?php } ?> data-info="<?php echo $payment['name'];?>">
          <p class="text"><?php if($payment['name'] =="Visabill"){echo L('_credit_');}elseif($payment['name'] == "Paypal"){echo L('_paypal_');}elseif($payment['name'] == "Worldpay"){echo L('_worldpay_');}elseif($payment['name'] == "Alipay"){echo L('_alipay_');};?></p>
          <img src="/statics/front/images/cart/<?php echo strtolower($payment['name']);?>.png" alt="<?php echo isset($payment['name']) ? $payment['name'] : null ;?>">
          <i class="mark"></i>
        </dd>
        <?php
              $i++;
            endforeach;
          endif;
        ?>
      </dl>
      <div class="caption" style="margin-bottom:2em;"><i class="serial-num">4</i><?php echo L('order_summary');?></div>
      <table class="align-center shopping-list">
        <tr>
          <th width="10%"><?php echo L('goods_img')?></th>
          <th width="40%">&nbsp;</th>
          <th width="10%"><?php echo L('goods_price')?></th>
          <th width="10%"><?php echo L('goods_number')?></th>
          <th width="20%"><?php echo L('amount_about')?></th>
          <th width="10%"><?php echo L('figure')?></th>
        </tr>
        <?php
          if ($cartInfo) :
          foreach ($cartInfo['productList'] as $k => $cart) :
          $product = $cart['product'];
        ?>
          <?php
            $advanced_price = $product['advanced_price'];
            $original_price = $product['original_price'];
          ?>
        <tr>
          <td>
            <a href="<?php echo url('product/'.$product['id'].'-'.generateUrl_un($product['english_name']));?>"><img src="<?php echo $product['small_img'] ?>"/></a>
          </td>
          <td class="align-left">
            <a href="<?php echo url('product/'.$product['id']).'-'.generateUrl_un($product['english_name']);?>" class="link-gray"><?php echo isset($product['name']) ? $product['name'] : null ?></a>
          </td>
          <td>
          <?php
          if($original_price > 0){
            $price = currency($product['original_price']);
            $new_price="<del>$price</del>";
            echo $new_price;}
          ?>
          <?php echo isset($product['price']) ? currency($product['price']) : null ?></td>
          <td><?php echo isset($cart['qty']) ? $cart['qty'] : null ?></td>
          <td>
            <span id="goods_amount_<?php echo $cart['id']; ?>"><?php echo currency($product['goods_amount']);?></span>
          </td>
          <td>
            <?php
              $measureInfo = AttributeModel::get_user_measure_info($cart['measure_id']);
                if($measureInfo['m_status'] == 5):
            ?>
            <p>
              <?php foreach(unserialize($measureInfo['measure_data']) as $mskey=>$msval):?>
              <?php echo MeasureModel::msizedataShow($mskey);?>:<strong class="text-bold"><?php echo $msval;?></strong><br />
              <?php endforeach;?>
            </p>
            <?php else:?>
		        <?php echo $measureInfo['measure_name'];?>
            <?php endif;?>
          </td>
        </tr>
        <?php if($cart['pants_qty'] > 0):
          $add_pants = $cart['add_pants'];
        ?>
        <tr>
          <td></td>
          <td class="align-left"><?php echo L('AdditionalPants'); ?></td>
          <td>
          <?php echo isset($add_pants['price']) ? currency($add_pants['price']) : null ?></td>
          <td><?php echo isset($cart['pants_qty']) ? $cart['pants_qty'] : null ?></td>
          <td>
            <span id="goods_amount_<?php echo $cart['id']; ?>_pants"><?php echo currency($add_pants['price'] * $cart['pants_qty']);?></span>
          </td>
          <td></td>
        </tr>
        <?php endif;?>
        <?php
            endforeach;
          endif;
        ?>
      </table>
      <p class="align-right" style="margin:1em 0 2em;">
        <a href="<?php echo url('cart/info');?>" class="font-md"><?php echo L('_back_cart_')?></a>
      </p>
      <dl class="definition-list message-box">
        <dt class="caption"><i>5</i><?php echo L('_order_msg_')?></dt>
        <dd class="textarea-box"><textarea name="remark"></textarea></dd>
      </dl>
      <p class="align-center">
        <input type="submit" name="dosubmit" value="<?php echo L('_PLACE_ORDER_');?>" class="btn btn-middle place-order js-place-order">
      </p>
    </div>
    <div class="col-md-4 bg-sliver">
      <div class="sidebar">
        <ul class="order-details">
          <li>
            <em><?php echo L('goods_amount_total')?>:</em>
            <strong id="cart_goods_amount"><?php echo currency($cartInfo['total_amount']) ?></strong>
          </li>
          <li>
            <i>+</i>
            <em><?php echo L('shipping_fee')?>:</em>
            <strong id="shipping_money"></strong>
          </li>
          <?php if($plus_size_Fee){?>
          <li>
            <i>+</i>
            <em>附加费:</em>
            <strong id="surcharge"><?php echo currency($plus_size_Fee);?></strong>
          </li>
          <?php }?>
          <?php if(isset($user['id']) && $user['id'] && floatval($user['user_money']) > 0 && C('is_use_surplus_money')):?>
          <li>
            <i>-</i>
            <em><?php echo L('usebalance')?>:</em>
            <strong class="surplus_money">0.00</strong>
          </li>
          <?php endif;?>
          <li>
            <i>-</i>
            <em><?php echo L('order_discount_you_save')?>:</em>
            <strong id="discount_money">0.00</strong>
          </li>
          <li class="order-amount">
            <em><?php echo L('order_to_pay_amount')?>:</em>
            <strong id="order_amount"><?php echo isset($cartInfo['total_amount']) ? currency($cartInfo['total_amount']) : null;?></strong>
          </li>
        </ul>
        <?php if(time()<strtotime('2015-01-26 13:00:00')): ?>
        <ul class="bonus-coupon js-bonus-coupon">
          <li<?php if(empty($self_coupon)):?> class="checked"<?php endif;?>>
            <span>
              <input type="radio" name="bonus_coupon" autocomplete="off"<?php if(empty($self_coupon)):?> checked<?php endif;?>>
              <?php echo L('_bonus_');?>
            </span>
            <div class="bonus-box">
              <select name="bonus" id="ECS_BONUS">
                <option value="0" selected="selected"><?php echo L('Please_select');?></option>
                <?php
                  foreach ($bonusList as $k => $bonus):
                ?>
                  <?php if($cartInfo['total_amount']-$special_total_amount-$cartInfo['gift_money'] >= $bonus['min_goods_amount'] ) {?>
                  <option value="<?php echo $bonus['id']?>"><?php echo $bonus['type_name']?>[<?php echo currency($bonus['type_money'])?>]</option>
                  <?php } ?>
                <?php
                  endforeach;
                ?>
              </select>
            </div>
          </li>
          <li<?php if(!empty($self_coupon)):?> class="checked"<?php endif;?>>
            <span>
              <input type="radio" name="bonus_coupon" autocomplete="off"<?php if(!empty($self_coupon)):?> checked<?php endif;?>>
              <?php echo L('coupon_code');?>
            </span>
            <div class="coupon-box">
              <input type="text" name="favorable" id="favorable">
              <button type="button" class="btn"><?php echo L('submit');?></button>
            </div>
          </li>
        </ul>
        <?php else: ?>
          <?php if(!$joinShirtCleanr):?>
            <div class="coupon-box js-coupon-box">
              <label>
                <input type="checkbox" autocomplete="off" class="vm">
                <em class="normal vm"><?php echo L('coupon_code');?></em>
              </label>
              <input type="text" name="favorable" id="favorable" value="<?php echo cookie("self_coupon")?cookie("self_coupon"):null?>">
              <button type="button"><?php echo L('submit');?></button>
            </div>
          <?php endif; ?>
        <?php endif; ?>
        <input type="submit" name="dosubmit" value="<?php echo L('_PLACE_ORDER_');?>" class="width-full mt1x mb1x btn btn-middle place-order js-place-order">
        <ul class="align-center shopping-clause">
          <li>
            <span><?php echo L('receiver_by_november');?></span>
            <p><img src="/statics/front/images/icons/lightning.png" alt="<?php echo L('receiver_by_november');?>"></p>
          </li>
          <li>
            <span><?php echo L('free_shipping');?></span>
            <p><img src="/statics/front/images/icons/shipment.png" alt="<?php echo L('free_shipping');?>"></p>
          </li>
          <li>
            <span><?php echo L('fit_promise');?></span>
            <p><img src="/statics/front/images/icons/fit.png" alt="<?php echo L('fit_promise');?>"></p>
          </li>
        </ul>
      </div>
    </div>
  </div>
</form>
<div id="addressBox" class="main-width align-center popup-box form-popup" style="display:<?php echo (isset($addressList) && $addressList) ? 'none' : 'block';?>;">
  <form class="addressForm" action="<?php echo url('cart/cart_address_edit');?>" name="dbfrom" method="post">
    <dl class="address-fields row">
      <dt class="title"><?php echo L('delivery_address');?></dt>
      <dd class="col-md-6">
        <label><?php echo L('fristname')?>: <input type="text" name="delivery_first_name" required id="delivery_first_name" /></label>
      </dd>
      <dd class="col-md-6">
        <label><?php echo L('state')?>: <input type="text" name="delivery_province" required id="delivery_province" /></label>
      </dd>
      <dd class="col-md-6">
        <label><?php echo L('city')?>: <input type="text" name="delivery_city" required id="delivery_city" /></label>
      </dd>
      <dd class="col-md-6">
        <label><?php echo L('_STREET_LINE1_');?>: <input type="text" name="delivery_address" required id="delivery_address" /></label>
      </dd>
      <dd class="col-md-12">
        <label><?php echo L('_STREET_LINE2_');?>: <input type="text" name="delivery_address2" required id="delivery_address2" /></label>
      </dd>
      <dd class="col-md-6">
        <label><?php echo L('phone')?>: <input type="text" name="delivery_phone" required id="delivery_phone" /></label>
      </dd>
      <dd class="col-md-6">
        <label><?php echo L('email')?>: <input type="text" name="delivery_email" required id="delivery_email" value="<?php echo $email_address;?>"/></label>
      </dd>
      <dd class="col-md-6 check-box">
        <label><input type="checkbox" name="default" checked /><?php echo L('default')?></label>
      </dd>
    </dl>
    <p>
      <input type="hidden" name="address_id" id="address_id" />
      <input type="submit" name="dosubmit" value="<?php echo L('submit')?>" class="btn btn-middle" />
      <span class="btn btn-middle js-close hide"><?php echo L('_PURCHASE_CANCAL_')?></span>
    </p>
    <i class="close js-close">×</i>
  </form>
</div>
<div class="mask js-mask" style="display:<?php echo (isset($addressList) && $addressList) ? 'none' : 'block';?>;"></div>
<?php include THEMES_PATH . 'public/footer2.phtml';?>
<?php include THEMES_PATH . 'public/after_body.phtml';?>
<script src="/statics/front/js/scripts/cart.js"></script>
<script>
$(function() {

  /*收货地址表单处理*/
  var addressBox = $("#addressBox"),
      addressForm = addressBox.find(".addressForm"),
      addressValidator = addressForm.validate({ //地址验证
        errorElement: 'p',
        submitHandler: function(form) {
          form.submit();
        },
        errorPlacement: function(error, element) {
          element.after(error).parent().addClass('error');
        },
        success: function(label) {
          label.parent().removeClass('error');
        },
        rules: {
          delivery_cid:{
            required: true,
            min:1
          },
          delivery_phone: {
            required: true
          },
          delivery_email: {
            required: true,
            email: true
          }
        },
        messages: {
          delivery_email: {
            requried: addressForm.find('input[name=delivery_email]').attr('title'),
            email: addressForm.find('input[name=delivery_email]').data('email')
          }
        }
      });

  position.fixPosition(addressBox);
  /*添加新的收货地址，显示地址弹窗*/
  function show_address_box() {
    addressBox.add($('.js-mask').last()).show();
    position.fixPosition(addressBox);
  }

  /*关闭地址弹窗*/
  function close_address_box() {
    addressForm[0].reset();
    addressForm.find("label.error").removeClass("error").end()[0].reset();
    addressValidator.resetForm();
    addressBox.add($('.js-mask')).hide();
  }

  /*编辑收货地址*/
  function editAddress(aid) {
    $.post(url('cart/get_address_info_ajax'), {
      aid: aid
    }, function(res) {
      if (res.error == 1) {
        alert(res.message);
      } else {
        var info = res.message;
        $.each(info, function (i,v) {
          addressForm.find("input:text[name='"+i+"']").val(v);
        });
        $('#adelivery_address_id').val(info.id);
        $('#address_id').val(aid);
        show_address_box();
      }
    },"json");
  }

  /*使用优惠码*/
  function changefavorable() {
    changeBonus(0);
    var is_user_money = $('#is_user_money:checked').val();
    var shipping_free = $('#input_shipping_money').val();
    var goods_amount = $('#goods_amount').val();
    var payment_fee = $('#input_payment_money').val();
    var integral_money = $('#input_integral_money').val();
    var hongbao_money = $('#input_hongbao_money').val();
    var coupon_money = $('#coupon_code_money').val();
    var use_integral = $('#use_integral').val();
    var favorable = $('#order_amount').text();
    var favorableCode = $('#favorable').val();

    if (isNaN(use_integral)) {
      $('#use_integral').val(0);
      use_integral = 0;
    }
    var amount = parseFloat(parseFloat(goods_amount) + parseFloat(payment_fee) + parseFloat(shipping_free) - parseFloat(hongbao_money));
    if (favorable) {
      $.get(url('cart/favorable/amount/' + amount + '/favorables/' + favorableCode), function(res) {
        var res = eval('(' + res + ')');
        if (res['error'] == 1) {
          var price = res['price'];
          paymentc($('#discount_money'), price);
          $('#coupon_code_money').val(price);
          $('#coupon_code').val(favorableCode);
          getSurplusMoney();
        } else {
          paymentc($('#discount_money'), 0);
          $('#coupon_code_money').val(0);
          $('#coupon_code').val('');
          getSurplusMoney();
          alert(res['errStr']);
        }
      });
    } else {
      paymentc($('#discount_money'), 0);
      $('#coupon_code_money').val(0);
      $('#coupon_code').val('');
      getSurplusMoney();
    }
  }

  //自动触发
  <?php if(!empty($self_coupon)):?>
  $('.js-coupon-box').find('input[type=checkbox]').click();
  $('#favorable').val('<?php echo $self_coupon;?>');
  changefavorable();
  <?php endif;?>

  <?php if(!empty($addressList)){?>
  getCartAddress();
  <?php }?>
  getshippingmoney();


  /*事件响应*/
  $('.js-add-address').on('click', function() { //添加地址
    show_address_box();
  });
  $('.js-edit-address').on('click', function() { //编辑地址
    if ($(this).parents('dd').index($('.js-address-list').children('dd')) === 0) {
      addressBox.find('dd').last().hide();
    } else {
      addressBox.find('dd').last().show().find('input[type=checkbox]').prop('checked', false);
    }
    editAddress($(this).parents('dd').data('info'));
  });
  $('.js-delete-address').on('click', function() { //删除地址
    var $this = $(this);
    $.get(url('user/address_delete'), {
      is_ajax: 1,
      aid: $this.parents('dd').data('info')
    }, function() {
      $this.parents('dd').remove();
    });
  });
  $('.js-close', addressBox).on('click', function() { //关闭弹框
     close_address_box();
  });

  $('.js-address-list').find('dd').on('click', function(event) { //选择地址
    var $this = $(this);
    if (!$(event.currentTarget).is($this.parent().children().last())) {
      $this.parent().find('dt > input[type=hidden]').val($this.data('info'));
      $this.addClass('selected').siblings().removeClass('selected');
      getCartAddress();
    }
  });

  $('.js-shipping-method').find('dd').on('click', function() { //选择发货方式
    var $this = $(this);
    $this.parent().find('dt > input[type=hidden]').val($this.data('info'));
    $this.addClass('selected').siblings().removeClass('selected');
    getshippingmoney();
  });

  $('.js-payment-method').find('dd').on('click', function() { //选择支付方式
    var $this = $(this);
    $this.parent().find('dt > input[type=hidden]').val($this.data('info'));
    $this.addClass('selected').siblings("dd").removeClass('selected');
    getPaymentMoney();
  });

  $('#is_user_money').on('click', function() { //余额支付
    var $this = $(this);
    if($this.is(":checked")){
      $this.parent().addClass("selected");
    }else{
      $this.parent().removeClass("selected");
    }
    getSurplusMoney();
  });

  $('#favorable').next('button').on('click', function() { //使用优惠码
    changefavorable();
  });

  $('#checkoutForm').find('.js-place-order').on('click', function() {//防止重复提交订单
    if ($(this).hasClass('disabled')) {
      return false;
    }
    $(this).addClass('disabled');
  });
});
</script>